//
//  ZYThirdShareManager.m
//  SuiTong
//
//  Created by 石志愿 on 2020/9/3.
//  Copyright © 2020 石志愿. All rights reserved.
//

#import "ZYShareManager.h"

#import "ZYTencentHandler.h"
#import "ZYWeChatHandler.h"
#import "ZYWeiBoHandler.h"
#import "ZYAppleLoginHandler.h"

#import "AFNetworking.h"

@interface ZYShareManager ()

@property (nonatomic, strong) ZYWeChatHandler *weChatHandler;
@property (nonatomic, strong) ZYTencentHandler *tencentHandler;
@property (nonatomic, strong) ZYWeiBoHandler *weiBoHandler;
@property (nonatomic, strong) ZYAppleLoginHandler *appleLoginHandler;

@end

@implementation ZYShareManager

static ZYShareManager *_shareManager = nil;

+ (instancetype)shared {
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _shareManager = [[super allocWithZone:NULL] init] ;
    }) ;
    
    return _shareManager;
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [ZYShareManager shared];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [ZYShareManager shared];
}

//MARK: public

- (void)registerSDK {
//    [self.tencentHandler registerApp];
    [self.weChatHandler registerApp];
//    [self.weiBoHandler registerApp];
}

- (BOOL)installQQ {
    return [self.tencentHandler installQQ];
}

- (BOOL)installWeChat {
    return [self.weChatHandler installWeChat];
}

- (BOOL)installWeiBo {
    return [self.weiBoHandler installWeiBo];
}

// 处理应用拉起协议
- (BOOL)HandleOpenURL:(NSURL *)url {
    if ([url.scheme hasPrefix:@"wx"]) {
        return [self.weChatHandler HandleOpenURL:url];
    } else if ([url.scheme hasPrefix:@"tencent"]) {
        return [ZYTencentHandler HandleOpenURL:url];
    } else if ([url.scheme hasPrefix:@"wb"]) {
        return [self.weiBoHandler HandleOpenURL:url];
    }
    return NO;
}

// 处理应用的UniversalLink拉起协议;
- (BOOL)HandleUniversalLink:(NSUserActivity *)userActivity {
    NSString *url = userActivity.webpageURL.absoluteString;
    if ([url containsString:kWxAppKey]) {
        return [self.weChatHandler HandleUniversalLink:userActivity];
    } else if ([url containsString:kQQAppKey]) {
        return [ZYTencentHandler handleUniversalLink:userActivity.webpageURL];
    }
    return NO;
}

//MARK: 分享

// 分享网页
- (void)shareWebPage:(ZYThirdShareInfo *)info platform:(ZYSharePlatform)platform completion:(__nullable ZYThirdShareBlock)completion {
    switch (platform) {
        case ZYSharePlatformQQ:
            [self.tencentHandler shareWebPage:info platform:platform completion:completion];
            break;
         case ZYSharePlatformQzone:
            [self.tencentHandler shareWebPage:info platform:platform completion:completion];
            break;
        case ZYSharePlatformWXSession:
            [self.weChatHandler shareWebPage:info platform:platform completion:completion];
            break;
        case ZYSharePlatformWXTimeline:
            [self.weChatHandler shareWebPage:info platform:platform completion:completion];
            break;
        case ZYSharePlatformWeiBo:
            [self.weiBoHandler shareWebPage:info completion:completion];
            break;
        default:
            break;
    }
}

//复制链接
- (void)copylink:(NSString *)link {
    if ([NSString isEmpty:link]) {
        [MBProgressHUD showMessage:@"链接不能为空"];
        return;
    }
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = link;
    if ([NSString isEmpty:pasteboard.string]) {
        [MBProgressHUD showMessage:@"复制失败"];
    } else {
        [MBProgressHUD showMessage:@"已复制"];
    }
}

// 分享图片
- (void)shareImage:(UIImage *)image thumbImage:(UIImage *)thumbImage  platform:(ZYSharePlatform)platform completion:(__nullable ZYThirdShareBlock)completion {
    switch (platform) {
        case ZYSharePlatformQQ:
            [self.tencentHandler shareImage:image thumbImage:thumbImage completion:completion];
            break;
        case ZYSharePlatformWXSession:
            [self.weChatHandler shareImage:image thumbImage:thumbImage platform:platform completion:completion];
            break;
        case ZYSharePlatformWXTimeline:
            [self.weChatHandler shareImage:image thumbImage:thumbImage platform:platform completion:completion];
            break;
        case ZYSharePlatformWeiBo:
            [self.weiBoHandler shareImage:image completion:completion];
            break;
        default:
            break;
    }
}

///分享文本
- (void)shareText:(NSString *)text platform:(ZYSharePlatform)platform completion:(__nullable ZYThirdShareBlock)completion {
    switch (platform) {
        case ZYSharePlatformQQ:
            [self.tencentHandler shareText:text completion:completion];
            break;
            break;
        case ZYSharePlatformWXSession:
            [self.weChatHandler shareText:text platform:platform completion:completion];
            break;
        case ZYSharePlatformWXTimeline:
            [self.weChatHandler shareText:text platform:platform completion:completion];
            break;
        case ZYSharePlatformWeiBo:
            [self.weiBoHandler shareText:text completion:completion];
            break;
        default:
            break;
    }
}


/// 登录
- (void)login:(ZYThirdLoginPlatform)platform completion:(__nullable ZYThirdLoginBlock)completion {
    switch (platform) {
        case ZYThirdLoginPlatformQQ:
            [self.tencentHandler login:completion];
            break;
        case ZYThirdLoginPlatformWeChat:
            [self.weChatHandler login:completion];
            break;
        case ZYThirdLoginPlatformWeiBo:
            [self.weiBoHandler login:completion];
            break;
        case ZYThirdLoginPlatformApple:
            [self.appleLoginHandler login:completion];
            break;
            
        default:
            break;
    }
}

//MARK: network

- (void)get:(NSString *)url parameters:(NSDictionary *)parameters showLoading:(BOOL)showLoading success:(void(^)(id response))success failure:(void (^)(NSError *error))failure {
    
    if (!url || url.length <= 0) {
        return;
    }
    if (showLoading) {
        [STLoadingView showLoading];
    }
    DEBUGLog(@"\n ==request:\n%@:\n%@", url, parameters);
    [[AFHTTPSessionManager manager] GET:url parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [STLoadingView hiddenLoading];
        success(responseObject);
        DEBUGLog(@"\n ==response :\n%@:\n%@", url, responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DEBUGLog(@"\n===========response===========\n%@:\n%@", url, error);
        [STLoadingView hiddenLoading];
        failure(error);
    }];
}

//MARK: getter

- (ZYWeChatHandler *)weChatHandler {
    if (!_weChatHandler) {
        _weChatHandler = [[ZYWeChatHandler alloc]init];
    }
    return _weChatHandler;
}

- (ZYTencentHandler *)tencentHandler {
    if (!_tencentHandler) {
        _tencentHandler = [[ZYTencentHandler alloc]init];
    }
    return _tencentHandler;
}

- (ZYWeiBoHandler *)weiBoHandler {
    if (!_weiBoHandler) {
        _weiBoHandler = [[ZYWeiBoHandler alloc]init];
    }
    return _weiBoHandler;
}

- (ZYAppleLoginHandler *)appleLoginHandler {
    if (!_appleLoginHandler) {
        _appleLoginHandler = [[ZYAppleLoginHandler alloc]init];
    }
    return _appleLoginHandler;
}

@end
